package practise.alogrithm.gotoSiliconValley.array;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xinlyu on 8/4/2014.
 */
public class MergeRange {
    public List<Range> merge(List<Range> rangeList) {
        int start = Integer.MAX_VALUE, end = -1;
        List<Range> rangeList1 = new ArrayList<>();
        for (Range range : rangeList) {
            if (rangeList1.size() == 0) {
                rangeList1.add(range);
            } else {
                if (range.getStart() <= rangeList1.get(rangeList1.size() - 1).getEnd()) {
                    Range temp = rangeList1.remove(rangeList1.size() - 1);
                    rangeList1.add(new Range(
                            Math.min(range.getStart(), temp.getStart()),
                            Math.max(range.getEnd(), temp.getEnd())
                    ));
                } else {
                    rangeList1.add(range);
                }
            }
        }
        return rangeList1;
    }

}

